//
//  BGThreeCell.m
//  bigMall
//
//  Created by ios on 16/9/18.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGThreeCell.h"
#import "BGId_ValueModel.h"
#import "BGReadStepTwoModel.h"
@interface BGThreeCell () {
    UILabel *lab110;
    UITextField *filed110;
    UILabel *titleLab;
    NSMutableArray *_dataArr;
    NSMutableArray *idArr;
    NSMutableArray *default_idArr;
}
@end

@implementation BGThreeCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier dataArr:(NSMutableArray *)marr default_value:(NSArray *)default_valueArr {
    NSLog(@"aaa");
    
    default_idArr = [NSMutableArray array];
    for (BGReadStepTwoModelArrDetail *model in default_valueArr) {
        [default_idArr addObject:model.ID];
    }
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _dataArr = [NSMutableArray array];
        idArr = [NSMutableArray array];
        for (BGReadStepTwoModelArrDetail *model in marr) {
            [_dataArr addObject:model.value];
            [idArr addObject:model.ID];
        }
        
        
        //修饰线
        UIView *v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 1)];
        v1.backgroundColor = dockwhite_;
        [self.contentView addSubview:v1];
        
        titleLab = [[UILabel alloc] initWithFrame:CGRectMake(21, 6, 100, 21)];
        titleLab.text = self.titleText;
        //设置标题的大小
        CGSize biaotiSize = [titleLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleLab.font,NSFontAttributeName, nil]];
        titleLab.width = biaotiSize.width;
        titleLab.height = biaotiSize.height;
        titleLab.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:titleLab];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 32, [UIScreen mainScreen].bounds.size.width, 1)];
        view.backgroundColor = dockwhite_;
        [self.contentView addSubview:view];
        
        for (int i = 0; i <marr.count; i++) {
            
            UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(21, 10 + 32 * (i + 1), 16, 16)];
            btn.tag = [idArr[i] intValue] + 1;
            btn.tintColor = [UIColor blueColor];
            btn.backgroundColor = dockwhite_;
            btn.clipsToBounds = YES;
            btn.layer.cornerRadius = 8;
            [btn addTarget:self action:@selector(clickChance:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:btn];
            
            lab110 = [[UILabel alloc] init];
            lab110.tag = btn.tag;
            lab110.font = [UIFont systemFontOfSize:14];
            lab110.text = _dataArr[i];
            CGSize size = [lab110.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:lab110.font,NSFontAttributeName, nil]];
            lab110.frame = CGRectMake(btn.right + 10, 10 + (i + 1) * 32, size.width, 16);
            [self.contentView addSubview:lab110];
            
            filed110 = [[UITextField alloc] initWithFrame:CGRectMake(lab110.right + 10, lab110.y, 100, 16)];
            filed110.borderStyle = UITextBorderStyleNone;
            filed110.placeholder = @"填写浮动值";
            filed110.font = [UIFont systemFontOfSize:14];
            filed110.userInteractionEnabled = NO;
            filed110.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
            filed110.tag = btn.tag;
            [filed110 addTarget:self action:@selector(filedChange:) forControlEvents:UIControlEventEditingDidEnd];
            [self.contentView addSubview:filed110];
            
            //开始赋默认
            for (BGReadStepTwoModelArrDetail *model in default_valueArr) {
                if ([model.ID intValue] == btn.tag - 1) {
                    
                    btn.selected = YES;
                    btn.backgroundColor = [UIColor redColor];
                    filed110.userInteractionEnabled = YES;
                    filed110.text = model.changePrice;
                    
                }
            }
            
        }
        
        

        
    }
    
    return self;
    
}
- (void)filedChange:(UITextField *)filed {
    for (UILabel *lab  in self.contentView.subviews) {
        
        if ([lab isKindOfClass:[UILabel class]]) {
//            NSLog(@"%ld",(long)lab.tag);
//            NSLog(@"%ld",(long)filed.tag);
            if (lab.tag == filed.tag) {
                
                if ([_delegate respondsToSelector:@selector(labValueChange:filedValueChange:index:)]) {
                    [_delegate labValueChange:lab.text filedValueChange:filed index:self.attr_id];
                }

               }
            
        }
        
        
    }

    
}
- (void)setTitleText:(NSString *)titleText {
    titleLab.text = titleText;
}
- (void)layoutSubviews {
    
    //设置标题的大小
    CGSize biaotiSize = [titleLab.text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleLab.font,NSFontAttributeName, nil]];
    titleLab.width = biaotiSize.width;
    titleLab.height = biaotiSize.height;
    
}
- (void)clickChance:(UIButton *)btn {
    
    btn.selected = !btn.selected;
    if (btn.selected) {
        [btn setBackgroundColor:[UIColor redColor]];
        
        for (UITextField *filed  in self.contentView.subviews) {
            
            if ([filed isKindOfClass:[UITextField class]]) {
                
                if (filed.tag == btn.tag) {
                    filed.userInteractionEnabled = YES;
                    filed.text = @"0.00";
                    [self filedChange:filed];
                }
                
            }
            
            
        }
        
        
        
        
        
    } else {
        [btn setBackgroundColor:dockwhite_];
        for (UIView *view in self.contentView.subviews) {
            if ([view isKindOfClass:[UITextField class]]) {
                UITextField *filed = (UITextField *)view;
                if (filed.tag == btn.tag) {
                    filed.userInteractionEnabled = NO;
                    filed.text = nil;
                    [self filedChange:filed];
                }
            }
        }
        
    }
    for (UIView *view in self.contentView.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            UILabel *lab = (UILabel *)view;
            if (lab.tag == btn.tag) {
               
                if ([_delegate respondsToSelector:@selector(valueChangeThree:lab:index:)]) {
                    [_delegate valueChangeThree:btn lab:lab index:self.attr_id];
                    //        NSLog(@"self.attr_id === %@",self.attr_id);
                }
                
            }
        }
    }

}
@end
